package com.iplant.base.utils.sql;
 

import javax.sql.DataSource;
 
import org.springframework.beans.factory.DisposableBean;

import com.iplant.base.po.enums.DBEnumType;
import com.iplant.base.utils.BaseConfigConstans;
import com.iplant.base.utils.general.LockHelper; 

public class DBHelper implements DisposableBean {
 

	private static LockHelper mLockHelper = LockHelper.getLock("DBDruidHelper");

	private static DataSource MySqlDataSource = null;

	private static DataSource GetMySqlDataSource() {
		if (MySqlDataSource == null) {
			synchronized (mLockHelper) {
				if (MySqlDataSource == null) {
					//MySqlDataSource = new ComboPooledDataSource("MySql_dataSource");
				}
			}
		}

		return MySqlDataSource;
	}

	private static DataSource AccessDataSource = null;

	private static DataSource GetAccessDataSource() {
		if (AccessDataSource == null) {
			synchronized (mLockHelper) {
				if (AccessDataSource == null) {
					//AccessDataSource = new ComboPooledDataSource("Access_dataSource");
				}
			}
		}

		return AccessDataSource;
	}

	private static DataSource OracleDataSource = null;

	private static DataSource GetOracleDataSource() {
		if (OracleDataSource == null) {
			synchronized (mLockHelper) {
				if (OracleDataSource == null) {
					//OracleDataSource = new ComboPooledDataSource("Oracle_dataSource");
				}
			}
		}

		return OracleDataSource;
	}

	private static DataSource SQLServerDataSource = null;

	private static DataSource GetSQLServerDataSource() {
		if (SQLServerDataSource == null) {
			synchronized (mLockHelper) {
				if (SQLServerDataSource == null) {
					//SQLServerDataSource = new ComboPooledDataSource("SQLServer_dataSource");
				}
			}

		}
		return SQLServerDataSource;
	}

	private static DBEnumType DBType = DBEnumType.getEnumType(BaseConfigConstans.DBServerType);

//	public static Connection getConnection(DBEnumType wDBType) throws SQLException {
//
//		DataSource wDataSource = getDataSource(wDBType);
//		return wDataSource.getConnection();
//
//	}

	public static DataSource getDataSource() {

		return getDataSource(DBType);
	}

	public static DataSource getDataSource(DBEnumType wDBType) {
		DataSource wResult = null;

		switch (wDBType) {
		case Default:
			wResult = GetMySqlDataSource();
			break;
		case Access:
			wResult = GetAccessDataSource();
			break;
		case MySQL:
			wResult = GetMySqlDataSource();
			break;
		case Oracle:
			wResult = GetOracleDataSource();
			break;
		case SQLServer:
			wResult = GetSQLServerDataSource();
			break;
		default:
			wResult = GetMySqlDataSource();
			break;
		}
		return wResult;
	}

//	public static ShrisJdbcTemplate getTemplate() {
//
//		return new ShrisJdbcTemplate(getDataSource(DBType), DBType);
//	}
//
//	public static ShrisJdbcTemplate getTemplate(DBEnumType wDBEnumType) {
//
//		return new ShrisJdbcTemplate(getDataSource(wDBEnumType), wDBEnumType);
//	}

	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub

//		try {
//			if (MySqlDataSource != null && MySqlDataSource instanceof ComboPooledDataSource) {
//				((ComboPooledDataSource) MySqlDataSource).close();
//			}
//		} catch (Exception e) {
//			logger.error("MySqlDataSource destroy",e);
//		}
//		try {
//			if (AccessDataSource != null && AccessDataSource instanceof ComboPooledDataSource) {
//				((ComboPooledDataSource) AccessDataSource).close();
//			}
//		} catch (Exception e) {
//			logger.error("AccessDataSource destroy",e);
//		}
//		try {
//			if (OracleDataSource != null && OracleDataSource instanceof ComboPooledDataSource) {
//				((ComboPooledDataSource) OracleDataSource).close();
//			}
//		} catch (Exception e) {
//			logger.error("OracleDataSource destroy",e);
//		}
//		try {
//			if (SQLServerDataSource != null && SQLServerDataSource instanceof ComboPooledDataSource) {
//				((ComboPooledDataSource) SQLServerDataSource).close();
//			}
//		} catch (Exception e) {
//			logger.error("SQLServerDataSource destroy",e);
//		}

	}

}
